package com.elysia.newsservice.handler;

import com.baomidou.mybatisplus.core.exceptions.MybatisPlusException;
import com.elysia.newsservice.result.Result;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

@RestControllerAdvice
@Slf4j
public class GlobalExceptionHandler {

    // 参数非法异常
    @ExceptionHandler(IllegalArgumentException.class)
    public Result<String> handleIllegalArgException(IllegalArgumentException ex) {
        log.error("参数异常：{}", ex.getMessage());
        return Result.error("参数错误：" + ex.getMessage());
    }

    // 所有未被单独捕获的异常
    @ExceptionHandler(Exception.class)
    public Result<String> handleOtherExceptions(Exception ex) {
        log.error("系统异常：", ex);
        return Result.error("系统异常，请稍后重试");
    }
}
